package com.kevin.action;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;

public class BuildSql {
	private String sql;
	private JdbcParams[] jdbcParams;
	
	private StringBuilder sqlBuilder;
	private ArrayList<JdbcParams> paramList;
	
	private boolean appendSqlEnable = true;
	
	private int onceCount;
	
	public BuildSql() {
		sqlBuilder = new StringBuilder();
		paramList = new ArrayList<>();
	}
	public BuildSql(String sql,JdbcParams[] jsbcParams) {
		this.sql = sql;
		this.jdbcParams = jsbcParams;
	}
		
	public void setAppendSqlEnable(boolean appendSqlEnable) {
		if(this.appendSqlEnable != appendSqlEnable){
			this.appendSqlEnable = appendSqlEnable;
			onceCount = paramList.size();	
		}
	}
	
	public int getOnceCount() {
		return onceCount;
	}
	public String getSql() {
		if(StringUtils.isEmpty(sql) && sqlBuilder.length() > 0){
			sql = sqlBuilder.toString();
		}
		return sql;
	}

	public JdbcParams[] getJdbcParams() {
		if((jdbcParams == null || jdbcParams.length == 0) && (paramList != null && paramList.size() > 0)){
			jdbcParams = paramList.toArray(new JdbcParams[paramList.size()]);
		}
		return jdbcParams;
	}

	public void appendSql(String sql){
		if(appendSqlEnable){
			sqlBuilder.append(sql);
		}
	}
	
	public BuildSql delete(int start,int end){
		sqlBuilder.delete(start, end);
		return this;
	}
	
	public int sqlLength(){
		return sqlBuilder.toString().length();
	}
	
	public void addParam(JdbcParams p){
		paramList.add(p);
	}
}
